package com.czk.java;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/12/14 18:52
 */
public class CalculatePi {
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf().setAppName("java").setMaster("spark://180.76.242.86:7077");
        JavaSparkContext jsc = new JavaSparkContext(sparkConf);
        List<Integer> l = new ArrayList<>(5);
        for (int i = 0; i < 5; i++) {
            l.add(i);
        }

        long count = jsc.parallelize(l).filter(i -> {
            double x = Math.random();
            double y = Math.random();
            return x*x + y*y < 1;
        }).count();
        System.out.println("Pi is roughly " + 4.0 * count / 5);
    }
}
